######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# table2-margin.R
#  :: runs first stage results for table 2, with the vote margin as outcome

# Notes: The main results that are interpreted are found in main_fstage1_fe_sumcl and main_fstage2_fe_sumcl

rm(list=ls())
setwd('~/Dropbox/rainReplication')

# loop over data and fixed effects specifications 
for(fes.type in 2:3){ 
non.missings = 4

source('prelimMain.R')

#detach(covs) 
# ANALYSIS
       
main_fstage1_fe=lm(dose ~ as.factor(fe_id_num)+d_inc+
	extremist + dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + turnout_prv +
	dose_prv + vote_prv + rain_day+rain_day_prev+rain_day_prev_prv, subset=full,data=covs)       
  
main_fstage2_fe=lm(dose ~ as.factor(fe_id_num)+d_inc+
	extremist + dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + turnout_prv +
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev+rain_weekend_prev_prv, subset=full,data=covs)	    
        
main_fstage1_fe_sum=summary(main_fstage1_fe)
main_fstage2_fe_sum=summary(main_fstage2_fe)

main_fstage1_fe_sumcl=coeftest.cluster(covs[full,],main_fstage1_fe,cluster1='as.factor(fe_id_num)')
main_fstage2_fe_sumcl=coeftest.cluster(covs[full,],main_fstage2_fe,cluster1='as.factor(fe_id_num)')

save(
main_fstage1_fe,
main_fstage2_fe,
main_fstage1_fe_sum,
main_fstage2_fe_sum,
main_fstage1_fe_sumcl,
main_fstage2_fe_sumcl,
file=paste('main/table2_margin-',non.missings,'_',fes.type,'.Rdata',sep='')) 

rm(
main_fstage1_fe,
main_fstage2_fe,
main_fstage1_fe_sum,
main_fstage2_fe_sum,
main_fstage1_fe_sumcl,
main_fstage2_fe_sumcl
)

}      

# END